
import pymysql

from config.config import get_db_config



# 创建连接池
def get_db_connection():

    bd_config=get_db_config()
    return pymysql.connect(
        host=bd_config["host"],
        user=bd_config["user"],
        password=bd_config['password'],
        database=bd_config['db'],
        port=bd_config['port'],
        charset=bd_config['charset'],
        cursorclass=pymysql.cursors.DictCursor
    )


if __name__ == "__main__":
    conn = get_db_connection()
    try:
        with conn.cursor() as cursor:
            # 参数化查询防止SQL注入
            cursor.execute("SELECT * FROM user WHERE role_id=%s", (2,))
            users = cursor.fetchall()
            print(users)
    finally:
        conn.close()